package banxi.ktv.hdao;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import banxi.ktv.domain.AbumHome;

public class HibernateHelper {
	private static final Log log = LogFactory.getLog(HibernateHelper.class);

	private static final SessionFactory sessionFactory;;

	private HibernateHelper(){}
	
	static{
	try {
			//sessionFactory = (SessionFactory) new InitialContext()
			//		.lookup("SessionFactory");
		sessionFactory = new Configuration().configure()
				.buildSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Could not locate SessionFactory", e);
			
			throw new HibernateException(
					"Could not locate SessionFactory",e);
			
		}
	}

	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}

	public static Session getSession() throws HibernateException {
		if(sessionFactory.getCurrentSession() != null){
			return sessionFactory.getCurrentSession();
		}
		return sessionFactory.openSession();
	}
}
